package com.woniu.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniu.Utiles.MD5Util;
import com.woniu.pojo.Staff;
import com.woniu.service.StaffService;

public class StaffRealm extends AuthorizingRealm{
	@Autowired
	StaffService staffService;
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "myRealm";
	}
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

		
		return null;
	}
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		System.out.println("doGetAuthenticationInfo");
		String userName=(String)token.getPrincipal();
		String passWord=String.valueOf((char[])token.getCredentials());
		Staff staff = new Staff();
		ByteSource salt = ByteSource.Util.bytes(userName);//���˺���Ϊ��ֵ
		staff.setStaff_job(userName);
		String password=MD5Util.getMD5String(salt, passWord);
		staff.setStaff_psw(passWord);
		Staff loginStaff = staffService.login(staff.getStaff_job(),staff.getStaff_psw());
		AuthenticationInfo info = new SimpleAuthenticationInfo(loginStaff,password,salt,getName());
		return info;
	}
}
